﻿using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Storage;

namespace BenevolentSunData
{
    public class PointLight
    {

        #region Light Properties
        Vector3 position;
        public Vector3 Position
        {
            get { return position; }
            set {
                position = value;
                //structPosition.SetValue(position);
                Invalidate();
            }
        }

        Color color;
        public Color Color
        {
            get { return color; }
            set {
                color = value;
                //structColor.SetValue(color.ToVector3());
                Invalidate();
            }
        }

        float range;
        public float Range
        {
            get { return range; }
            set { 
                range = value;
                //structRange.SetValue(range);
                Invalidate();
            }
        }

        float fallOff;
        public float FallOff
        {
            get { return fallOff; }
            set {
                fallOff = value;
                //structFallOff.SetValue(fallOff);
                Invalidate();
            }
        }
        #endregion


        #region Effect
        Effect effect;

        EffectParameter structInstance;

        EffectParameter structPosition;

        EffectParameter structColor;

        EffectParameter structRange;

        EffectParameter structFallOff;

        int lightIndex;
        #endregion


        #region Initialization
        public PointLight(Effect effect)
        {
            this.effect = effect;
            /*
            structInstance = effect.Parameters["PointLights"].Elements[0];
            structPosition = structInstance.StructureMembers["Position"];
            structColor = structInstance.StructureMembers["Color"];
            structRange = structInstance.StructureMembers["Range"];
            structFallOff = structInstance.StructureMembers["FallOff"];
            */
            position = Vector3.Zero;
            range = 30f;
            fallOff = 2f;
            color = Color.White;

            lightIndex = -1;
        }

        public PointLight()
        {
            position = Vector3.Zero;
            range = 30f;
            fallOff = 2f;
            color = Color.White;
        }

        public void Initialize(Effect effect)
        {
            this.effect = effect;
            
            /*
            structInstance = effect.Parameters["PointLights"].Elements[lightIndex];
            structPosition = structInstance.StructureMembers["Position"];
            structColor = structInstance.StructureMembers["Color"];
            structRange = structInstance.StructureMembers["Range"];
            structFallOff = structInstance.StructureMembers["FallOff"];
            */
        }
        #endregion

        /*
        public void UpdateLight()
        {
            structColor.SetValue(color.ToVector3());
            structFallOff.SetValue(fallOff);
            structPosition.SetValue(position);
            structRange.SetValue(range);
        }
        */
        public void UpdateLight(int lightIndex)
        {
            if (this.lightIndex == lightIndex) {
                //UpdateLight();
                return;
            }
            this.lightIndex = lightIndex;

            structInstance = effect.Parameters["PointLights"].Elements[lightIndex];
            structPosition = structInstance.StructureMembers["Position"];
            structColor = structInstance.StructureMembers["Color"];
            structRange = structInstance.StructureMembers["Range"];
            structFallOff = structInstance.StructureMembers["FallOff"];

            structColor.SetValue(color.ToVector3());
            structFallOff.SetValue(fallOff);
            structPosition.SetValue(position);
            structRange.SetValue(range);
        }


        public void Invalidate()
        {
            lightIndex = -1;
        }
    }
}
